<template>
  <div class="server-main">
    <el-form :inline="true" v-model="modelData" class="search-form">
      <el-form-item label="测试对象" class="form-input">
        <el-input v-model.trim="modelData.objectName" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" @click="onSubmit(1)"
          >查询</el-button
        >
      </el-form-item>
    </el-form>
    <div class="server-table">
      <div class="table-title">
        <span class="table-title-text">报告生成</span>
        <div>
          <el-upload
            class="upload-demo"
            :action="`${this.$api.resultAnalyse.zehaoObj.fileUpload}`"
            multiple
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
          >
            <el-button size="small" type="primary">导入测试数据</el-button>
          </el-upload>
        </div>
      </div>
      <el-table :data="tableList" class="km-table">
        <el-table-column
          type="index"
          label="ID"
          width="50"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="objectName"
          label="测试对象名称"
          align="center"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="itemCount"
          label="需求数量"
          align="center"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="testCaseCount"
          label="测试用例数量"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="testResultCount"
          label="测试结果数量"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="updateTime"
          label="最新下载报告时间"
          width="155"
          align="center"
        ></el-table-column>
        <!-- <el-table-column prop="ontologyAllState" label="状态" width="60" align="center">
          <template slot-scope="scope">
            <span>{{ +scope.row.ontologyAllState === 0 ? "启用" : "禁用" }}</span>
          </template>
        </el-table-column> -->
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-link
              :underline="false"
              type="primary"
              class="ontology-edit"
              @click="downloadFile(scope.row.reportUrl, scope.row.reportId)"
              >下载报告</el-link
            >
            <el-link
              :underline="false"
              type="primary"
              class="ontology-edit"
              @click.native.prevent="record(scope.row.reportId)"
              >下载记录</el-link
            >
            <el-link
              :underline="false"
              type="primary"
              class="ontology-edit"
              @click="delete1(scope.row.reportId)"
              >删除</el-link
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="km-pagination"
        background
        :small="true"
        layout="total, prev, pager, next,sizes, jumper"
        :total="total"
        :hide-on-single-page="!total"
        :page-size.sync="modelData.pageSize"
        :pager-count="7"
        :page-sizes="[10, 20, 30]"
        :current-page="modelData.pageNum"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
      >
      </el-pagination>
    </div>
  </div>
</template>
<script>
import { mapState, mapMutations } from "vuex";
export default {
  name: "OntologyManagement",
  components: {},
  data: () => {
    return {
      modelData: {
        objectName: "", //测试对象名称
        // ontologyAllName: "", // 本体名称
        // ontologyAllState: "2", // 状态 0 启用，1禁用，2全部
        pageNum: 1, // 页码
        pageSize: 10, // 每页条数
      },
      deleteModel: {
        reportId: "",
      },
      downloadModel: {
        reportId: "",
      },
      tableList: [], // 本体列表
      total: 0, // 数据总条数
      loading: false,
      dialogFormVisible: false,
      uploadFormVisible: false,
      editOntologyId: null, // 编辑本体的id
      modelParams: {
        ontologyAllName: "", // 本体名称
        ontologyAllDescribe: "", // 本体描述
      },
      dialogType: "ADD",
      fileModel: {
        file_url: "",
      },
      textSizeConfig: {
        minRows: 4,
        maxRows: 10,
      },
      uploadParams: {
        fileName: null,
      },
    };
  },
  computed: {
    ...mapState({
      containerDom: (state) => state.server.containerDom,
    }),
  },
  created() {
    // 获取报告列表
    this.getModelList();
  },
  methods: {
    ...mapMutations({
      // save_allGraphData: "ontologyModel/save_allGraphData",
      save_ontologyAllId: "ontologyModel/save_ontologyAllId",
    }),
    // 获取事件查询列表
    getModelList() {
      let params = this.modelData;
      this.$axios
        .post(this.$api.resultAnalyse.zehaoObj.reportGenerate, params)
        .then((res) => {
          let data = res.data.data.list;
          this.tableList = data;
          this.total = res.data.data.total;
          // this.modelData.pageSize = data.pageSize;
          // this.modelData.pageNum = data.pageNum;
          // this.$utils.backtop(this.containerDom);
        })
        .catch((err) => {
          console.error(err);
        });
    },
    // 查询
    onSubmit(pageNum) {
      this.modelData.pageNum = pageNum ? pageNum : this.modelData.pageNum;
      this.getModelList();
    },
    // 修改当前页码
    handleCurrentChange(page) {
      this.modelData.pageNum = page;
      this.getModelList();
    },
    //修改当前页面容量
    handleSizeChange(page) {
      this.modelData.pageSize = page;
      this.getModelList();
    },
    //删除
    delete1(id) {
      console.log("id", id);
      this.deleteModel.reportId = id;
      this.deleteResult();
    },
    deleteResult() {
      let params = this.deleteModel;
      this.$confirm(
        "是否确认删除此记录？删除后不可恢复，请谨慎操作。",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          //删除的主体方法
          this.$axios
            .post(this.$api.resultAnalyse.zehaoObj.deleteReport, params)
            .then((res) => {
              this.getModelList();
            })
            .catch((err) => {
              console.error(err);
            });
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //处理文件上传
    handleAvatarSuccess(res, file) {
      this.fileModel.file_url = res;
      let params = this.fileModel;
      this.$axios
        .post(this.$api.resultAnalyse.zehaoObj.handleReportFile, params)
        .then((res) => {
          this.$message.warning(res.data.message);
          this.getModelList();
        })
        .catch((err) => {
          console.error(err);
        });
    },
    //下载报告
    downloadFile(reportUrl, reportId) {
      if (reportUrl) {
        var iframe = document.createElement("iframe");
        iframe.style.display = "none";
        iframe.src = reportUrl;
        document.body.appendChild(iframe);
        this.downloadModel.reportId = reportId;
        let params = this.downloadModel;
        this.$axios
          .post(this.$api.resultAnalyse.zehaoObj.reportDownload, params)
          .then((res) => {
            console.log("res", res);
          })
          .catch((err) => {
            console.error(err);
          });
      } else {
        this.$message.warning("无可用的下载链接");
      }
    },
    //报告下载记录
    record(reportId) {
      this.$router.push({
        path: "/resultAnalyse/reportdlhistory",
        query: {
          reportId,
        },
      });
    },

    // 修改状态
    changeStatus(item) {
      console.log("ontologyAllState", item, item.ontologyAllState);
      let { ontologyAllId, ontologyAllState } = item;
      let params = {
        ontologyAllId,
        ontologyAllState: +ontologyAllState === 0 ? 1 : 0,
      };
      if (+ontologyAllState === 0) {
        // 点击禁用时
        this.$confirm("是否禁用当前本体?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            this.updateStatus(params);
          })
          .catch(() => {});
      } else {
        this.updateStatus(params);
      }
    },
    updateStatus(params) {
      this.$axios
        .post(this.$api.ontologyModel.xinbiaoObj.editOntology, params)
        .then(() => {
          this.$message.success("状态更改成功");
          this.onSubmit();
        })
        .catch((err) => {
          this.$message.success("状态更改失败");
          console.error(err);
        });
    },
    // 打开编辑本体框
    openEditOntology(row) {
      let { ontologyAllId, ontologyAllName, ontologyAllDescribe } = row;
      this.modelParams.ontologyAllName = ontologyAllName;
      this.modelParams.ontologyAllDescribe = ontologyAllDescribe;
      this.editOntologyId = ontologyAllId;
      this.dialogFormVisible = true;
      this.dialogType = "EDIT";
    },
    // 模态框关闭后
    afterDialogClose() {
      this.$refs.dialogForm && this.$refs.dialogForm.resetFields();
      this.$refs.uploadForm && this.$refs.uploadForm.resetFields();
    },
    // 打开新增本体框
    openCreateOntology() {
      this.dialogType = "ADD";
      this.dialogFormVisible = true;
    },
    // 取消
    resetDialogForm() {
      this.dialogFormVisible = false;
      this.uploadFormVisible = false;
    },
    // 确定
    submitDialogForm() {
      this.$refs.dialogForm.validate((valid) => {
        if (valid) {
          console.log("param", JSON.stringify(this.modelParams));
          let func = this.dialogConfig[this.dialogType].funcName;
          this[func]();
          this.dialogFormVisible = false;
        } else {
          return false;
        }
      });
    },
    // 创建本体

    // 批量导入
    uploadModel(row) {
      this.uploadFormVisible = true;
      this.editOntologyId = row.ontologyAllId;
    },
    beforeUpload(file) {
      this.uploadParams.fileName = file.name;
      this.uploadFile = file;
      return false;
    },
    submitUploadForm() {
      this.loading = true;
      this.$refs.uploadForm.validate((valid) => {
        if (valid) {
          let params = {
            file: this.uploadFile,
            ontologyAllId: this.editOntologyId,
          };
          this.$fileHttp
            .uploadFile(this.$api.ontologyModel.xuekuiObj.uploladModel, params)
            .then((res) => {
              let code = res.data.code;
              if (code !== 200) {
                let message = res.data.message;
                this.$message.warning(message);
              }
              this.loading = false;
              this.uploadFormVisible = false;
            });
        } else {
          this.loading = false;
          return false;
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.server-main {
  position: relative;

  .search-form {
    background: #ffffff;
    border-radius: 4px;
    padding: 16px 32px;
    .el-form-item {
      margin-bottom: 0;
    }

    .form-input {
      margin-right: 32px;
      .el-input,
      .el-select {
        ::v-deep .el-input__inner {
          width: 240px;
          height: 32px;
          background: #ffffff;
          border-radius: 2px;
          border: 1px solid #e0e0e0;
        }
      }
    }
  }

  .server-table {
    margin-top: 16px;
    padding: 16px 32px 64px;
    background: #ffffff;
    border-radius: 4px;

    .table-title {
      display: flex;
      justify-content: space-between;
      align-items: center;

      .table-title-text {
        margin: 8px 0;
        font-size: 16px;
        line-height: 16px;
        border-left: 5px solid $--color-primary;
        padding-left: 10px;
      }

      margin-bottom: 10px;
    }
  }

  .ontology-edit {
    padding: 0 10px;
    & + .ontology-edit {
      border-left: 1px solid #dddddd;
    }
  }

  ::v-deep .el-table__append-wrapper {
    padding: 10px;
  }

  ::v-deep .el-dialog {
    width: 500px;
    border-radius: 5px;
  }

  .dialog-inline {
    ::v-deep .el-form-item__content {
      width: 300px;
    }

    button.el-button {
      background-color: $--color-primary !important;
      color: #fff !important;
      line-height: 1.2;
    }
  }
  .upload-hint {
    font-size: 12px;
    color: #aaa;
    text-align: center;
    line-height: 40px;
  }
}
</style>
